package musicstore.web.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import musicstore.biz.BizException;
import musicstore.biz.GenreBiz;
import musicstore.entity.Genre;

@Controller
@RequestMapping("/user/genre-list")
public class GenreRestController {
	@Autowired
	private GenreBiz genrebiz;
	
	@RequestMapping(value="",method=RequestMethod.GET)
	@ResponseBody
	public List<Genre> list(){
		return genrebiz.getAll();
		
	}
	@ResponseBody
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	public JsonResult delete(@PathVariable int id){
		try {
			genrebiz.delete(id);
			return new JsonResult(true, null);
		} catch (BizException e) {
			return new JsonResult(false, e.getMessage());
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/{id}", method=RequestMethod.GET)
	public Genre single(@PathVariable int id){
		return genrebiz.fetchById(id);
	}

	@ResponseBody
	@RequestMapping(value="", method=RequestMethod.POST)
	public JsonResult save(Genre genre){
		try{
			if(genre.getId()>0)
				genrebiz.update(genre);
			else
				genrebiz.add(genre);
			return new JsonResult(true, null);
		}catch (BizException e) {
			return new JsonResult(false, e.getMessage());
		}
	}
/*	@ResponseBody
	@RequestMapping(value="/{name}", method=RequestMethod.GET)
	public Genre name(@PathVariable String name){
		return genrebiz.fetchByTitle(name);
	}*/
	
}
